package io.zw.modules.xinpai.service.impl;

import io.zw.modules.xinpai.service.CarProjectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.zw.common.utils.PageUtils;
import io.zw.common.utils.Query;

import io.zw.modules.xinpai.dao.CarDao;
import io.zw.modules.xinpai.entity.CarEntity;
import io.zw.modules.xinpai.service.CarService;
import org.springframework.transaction.annotation.Transactional;


@Service("carService")
public class CarServiceImpl extends ServiceImpl<CarDao, CarEntity> implements CarService {

    @Autowired
    private CarProjectService carProjectService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String imei= (String) params.get("imei");
        String plate_num=(String) params.get("plateNum");
        Page<CarEntity> page = this.selectPage(
                new Query<CarEntity>(params).getPage(),
                new EntityWrapper<CarEntity>().eq("firm_id",params.get("firm_id")).like(StringUtils.isNotBlank(plate_num),"plate_num",plate_num).like(StringUtils.isNotBlank(imei),"imei",imei)
        );
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CarEntity car) {
        this.insert(car);
        //保存区域关系
        carProjectService.saveOrUpdate(car.getImei(),car.getAreaList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CarEntity car) {
        this.updateById(car);
        //保存区域关系
        carProjectService.saveOrUpdate(car.getImei(),car.getAreaList());
    }

    @Override
    public List<Long> queryAreaList(String imei) {
        return this.baseMapper.queryAreaList(imei);
    }

}
